package org.zizi.hessian;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.zizi.dao.VatCreditDao;
import org.zizi.dao.VatTypeDao;
import org.zizi.manager.Managers;
import org.zizi.manager.VatCreditManager;
import org.zizi.manager.impl.DaoManager;
import org.zizi.modal.VatCredit;
import org.zizi.modal.VatCreditInvoice;
import org.zizi.modal.VatType;
import org.zizi.util.NumberFormat;

public class VatOutApiImpl implements VatOutApi
{
	private final static Logger log = Logger.getLogger(VatOutApiImpl.class);
	
	public void submit(VatCredit credit) throws ApiException
	{
		DaoManager dm = Managers.get(DaoManager.class);
		try
		{
			dm.begin();
			
			VatCreditDao vcDao = dm.getDao(VatCreditDao.class);
			
			Date now = new Date();
			
			VatCredit _new = null;
			if (credit.getId() > 0)
			{
				_new = vcDao.getVatCredit(credit.getId());
				if (_new == null) throw new Exception("找不到ID=" + credit.getId() + "的抵减清单");
			}
			else
			{
				_new = new VatCredit();
				
				_new.setCreatorId(null);
				_new.setSubmitTime(now);
				_new.setSubmitIp("通过接口提交");
			}
			
			_new.setLastModifyId(null);
			_new.setLastModifyTime(now);
			_new.setValid(true);
			
			_new.setYear(credit.getYear());
			_new.setMonth(credit.getMonth());
			_new.setIdentify(credit.getIdentify());
			
			//发票
			List<VatCreditInvoice> invoices = new ArrayList<VatCreditInvoice>();
			if (credit.getInvoices() != null)
			{
				for (VatCreditInvoice s : credit.getInvoices())
				{
					if (s.getInvoiceTypeId() == null || s.getInvoiceTypeId() <= 0) continue;
					s.checkInvoiceCategory();
					
					invoices.add(s);
				}
			}
			_new.setInvoices(invoices);
			
			_new.setApplyVatCredit(NumberFormat.format(credit.getApplyVatCredit()));
			
			VatCreditManager vcm = Managers.get(VatCreditManager.class);
			vcm.check(_new, false);	//检查正确性
			
			long creditId = vcDao.addOrUpdateVatCredit(_new);
			vcDao.updateVatCreditInvoices(creditId, invoices);
			
			dm.commit();
		}
		catch (Exception e)
		{
			throw new ApiException(e.getMessage());
		}
		finally
		{
			dm.end();
		}
	}
	
	public VatCredit getSubmitHistory(String identify, int year, int month) throws ApiException
	{
		DaoManager dm = Managers.get(DaoManager.class);
		try
		{
			dm.begin();
			
			VatCreditDao vcDao = dm.getDao(VatCreditDao.class);
			VatCredit v = vcDao.getVatCredit(identify, year, month);
			if (v != null) v.setInvoices(vcDao.getVatCreditInvoices(v.getId()));
			
			dm.commit();
			
			return v;
		}
		catch (Exception e)
		{
			throw new ApiException(e.getMessage());
		}
		finally
		{
			dm.end();
		}
	}
	
	public Double getApplyVatCredit(String identify, int year, int month) throws ApiException
	{
		log.info("getApplyVatCredit: " + identify + ", year=" + year + ", month=" + month);
		
		DaoManager dm = Managers.get(DaoManager.class);
		try
		{
			dm.begin();
			
			VatCreditDao vcDao = dm.getDao(VatCreditDao.class);
			VatCredit v = vcDao.getVatCredit(identify, year, month);
			
			dm.commit();
			
			double value = 0;
			if (v != null && v.getApplyVatCredit() != null) value = v.getApplyVatCredit();
			
			log.info("return value: " + value);
			
			return value;
		}
		catch (Exception e)
		{
			log.info("getApplyVatCredit error: " + e.getMessage());
			throw new ApiException(e.getMessage());
		}
		finally
		{
			dm.end();
		}
	}
	
	public VatType[] getAllVatType() throws Exception
	{
		DaoManager dm = Managers.get(DaoManager.class);
		try
		{
			dm.begin();
			
			VatTypeDao vcDao = dm.getDao(VatTypeDao.class);
			List<VatType> types = vcDao.listAllVatTypes(true);
			
			dm.commit();
			
			return types.toArray(new VatType[0]);
		}
		finally
		{
			dm.end();
		}
	}
}
